package com.mqz.seckill.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mqz.seckill.exception.GlobalException;
import com.mqz.seckill.mapper.UserMapper;
import com.mqz.seckill.pojo.User;
import com.mqz.seckill.service.UserService;
import com.mqz.seckill.util.CookieUtil;
import com.mqz.seckill.util.MD5Util;
import com.mqz.seckill.util.UUIDUtil;
import com.mqz.seckill.util.ValidatorUtil;
import com.mqz.seckill.vo.LoginVO;
import com.mqz.seckill.vo.RespBean;
import com.mqz.seckill.vo.RespBeanEnum;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class UserServiceImpl
        extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public RespBean doLogin(LoginVO loginVO, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();

//      使用注解校验
//        if (!(StringUtils.hasText(mobile) && StringUtils.hasText(password))) {
//            return RespBean.error(RespBeanEnum.MOBILE_NOT_EXIST_ERROR);
//        }
//        //验证手机号格式
//        if (!ValidatorUtil.isMobile(mobile)) {
//            return RespBean.error(RespBeanEnum.MOBILE_FORMAT_ERROR);
//        }

        //从db查找手机号
        User user = userMapper.selectById(mobile);
        if (null == user) {
//            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        //验证密码
        String finalPasswd = MD5Util.midToFinalPasswd(password, user.getSalt());
        if (!finalPasswd.equals(user.getPassword())) {
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }

        String userTicket = UUIDUtil.uuid();
//        request.getSession().setAttribute(userTicket, user);
        redisTemplate.opsForValue().set("user:" + userTicket, user);

        //保存ticket到cookie
        CookieUtil.setCookie(request, response, "userTicket", userTicket);

        return RespBean.success(userTicket);
    }

    @Override
    public User getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if (!StringUtils.hasText(userTicket)) {
            return null;
        }

        User user = (User) redisTemplate.opsForValue().get("user:" + userTicket);

        if (null != user) {
            CookieUtil.setCookie(request, response, "userTicket", userTicket);
        }

        return user;
    }

    @Override
    public RespBean updatePassword(String userTicket, String password, HttpServletRequest request, HttpServletResponse response) {

        User user = getUserByCookie(userTicket, request, response);

        if (user == null) {
            throw new GlobalException(RespBeanEnum.MOBILE_NOT_EXIST_ERROR);
        }

        user.setPassword(MD5Util.initToFinalPasswd(password, user.getSalt()));
        if (userMapper.updateById(user) == 1) {
            redisTemplate.delete("user:" + userTicket);
            return RespBean.success();
        }

        return RespBean.error(RespBeanEnum.PASSWORD_UPDATE_ERROR);
    }


}
